package com.sjy.project.fym.api;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.sjy.framework.aspectj.lang.annotation.Log;
import com.sjy.framework.aspectj.lang.enums.BusinessType;
import com.sjy.project.fym.apply.domain.FyGoods;
import com.sjy.project.fym.apply.domain.FyGoodsApply;
import com.sjy.project.fym.apply.domain.FyGoodsApplyHis;
import com.sjy.project.fym.apply.service.IFyGoodsApplyHisService;
import com.sjy.project.fym.apply.service.IFyGoodsApplyService;
import com.sjy.project.fym.apply.service.IFyGoodsService;
import com.sjy.project.fym.place.domain.FyPlace;
import com.sjy.project.fym.place.service.IFyPlaceService;
import com.sjy.project.fym.user.domain.FyUser;
import com.sjy.project.fym.user.service.IFyUserService;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.BetweenFormater.Level;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import springfox.documentation.annotations.ApiIgnore;

import com.sjy.framework.web.controller.BaseController;
import com.sjy.framework.web.domain.AjaxResult;
import com.alibaba.fastjson.JSONArray;
import com.sjy.common.constant.UserContext;
import com.sjy.common.entity.SecuryUserDetail;
import com.sjy.common.result.BaseResponse;
import com.sjy.common.utils.poi.ExcelUtil;
import com.sjy.common.utils.security.ShiroUtils;
import com.sjy.framework.web.page.TableDataInfo;

/**
 * 防疫物资申请Controller
 * 
 * @author ljb
 * @date 2022-04-20
 */
@RestController
@Api(value = "经开区防疫物资上报", description = "物资上报相关接口", tags = {"经开区防疫"})
@RequestMapping("/api/fym/apply")
public class ApiFyGoodsApplyController extends BaseController
{
    @Autowired
    private IFyGoodsApplyService applyService;
    @Autowired
    private IFyGoodsService goodsService;
    @Autowired
    private IFyGoodsApplyHisService hisService;
    @Autowired
    private IFyUserService fyUserService;
    @Autowired
    private IFyPlaceService fyPlaceService;
    
    @ApiOperation(value = "501、防疫物资申请上报", position = 501, notes = "物资内容动态传入")
    @ApiImplicitParams({
        @ApiImplicitParam(required=true,name="fyGoodsStr",value="json格式字符串",example="[{\"goodsId\":\"1\",\"applyId\":\"2\",\"goodsType\":\"1\",\"goodsNum\":100}]"),
    })
    @PostMapping({"/firstApply"})
    public ResponseEntity<BaseResponse<FyGoodsApply>> firstApply(String fyGoodsStr) throws Exception
    { 
    	try {
    		List<FyGoods> goodsList = JSONArray.parseArray(fyGoodsStr, FyGoods.class);
        	SecuryUserDetail user = UserContext.getUserSession();
        	boolean flag = applyService.firstApply(user,goodsList);
        	if(flag) {
            	return BaseResponse.generateOKResponseEntity("物资申报成功"); 
        	}
	    	return BaseResponse.generateBadResponseEntity("上报失败",null);
		} catch (Exception e) {
	    	return BaseResponse.generateBadResponseEntity("上报失败",e.getMessage());
		}
    }
    
    @ApiOperation(value = "502、根据审批节点查看用户自己当前签到场所的申请记录", position = 502, notes = "节点类型 是1234 2022年5月6日00:31:10由人员所在场所改成人员签到场所；清单随着签到场所变化而变化")
    @ApiImplicitParams({
        @ApiImplicitParam(required=false,name="applyStatus",value="当前审批节点 1234",example="1"),
        @ApiImplicitParam(required=false,name="searchValue",value="搜索内容",example="1"),
    })
    @PostMapping({"/applyListByStatus"})
    public ResponseEntity<BaseResponse<List<FyGoodsApply>>> applyListByStatus(@ApiIgnore FyGoodsApply fyGoodsApply) throws Exception
    { 
    	SecuryUserDetail user = UserContext.getUserSession();
    	//FyUser fyUser = fyUserService.selectFyUserByUserId(user.getUserId());
    	//fyGoodsApply.setCreateBy(user.getUserId()+"");//查询当前token对应用户下的信息
    	FyPlace fyPlace = fyPlaceService.selectFyPlaceBySigninUserId(user.getUserId());
    	if(fyPlace == null) {
    		return BaseResponse.generateBadResponseEntity("签到才可查看!", null);
    	}
    	fyGoodsApply.setPlaceId(fyPlace.getPlaceId());

    	startPage();
        List<FyGoodsApply> list = applyService.selectFyGoodsApplyList(fyGoodsApply);
        list.forEach(n->{
        	FyGoods goods = new FyGoods();
        	goods.setApplyId(n.getApplyId());
        	n.setFyGoods(goodsService.selectFyGoodsList(goods));
        	n.setCreater(fyUserService.selectFyUserByUserId(Long.parseLong(n.getCreateBy())));
        });
        return BaseResponse.generateOKResponseEntity(getDataTable(list));
    }
    
    @ApiOperation(value = "503、根据审批节点查看申请详情", position = 503, notes = "")
    @ApiImplicitParams({
        @ApiImplicitParam(required=false,name="applyId",value="审批ID",example="1"),
    })
    @PostMapping({"/applyDetail"})
    public ResponseEntity<BaseResponse<FyGoodsApply>> applyDetail(@RequestParam Long applyId) throws Exception
    { 
//    	startPage();
//    	SecuryUserDetail user = UserContext.getUserSession();
//    	FyUser fyUser = fyUserService.selectFyUserByUserId(user.getUserId());
    	
        FyGoodsApply apply = applyService.selectFyGoodsApplyByApplyId(applyId);
        if(apply==null) {
        	return BaseResponse.generateBadResponseEntity("记录无效", null);
        }
        FyGoods goods = new FyGoods();
    	goods.setApplyId(applyId);
        apply.setFyGoods(goodsService.selectFyGoodsList(goods));
        apply.setCreater(fyUserService.selectFyUserByUserId(Long.parseLong(apply.getCreateBy())));
        
        FyGoodsApplyHis fyGoodsApplyHis = new FyGoodsApplyHis();
        fyGoodsApplyHis.setApplyId(apply.getApplyId());
        apply.setFyGoodsApplyHis(hisService.selectFyGoodsApplyHisList(fyGoodsApplyHis));
        return BaseResponse.generateOKResponseEntity(apply);
    }
    
    
    @ApiOperation(value = "504、确认接收物资接口", position = 504, notes = "需要外填写 是否物资完备,物资接收图片")
    @ApiImplicitParams({
        @ApiImplicitParam(required=false,name="applyId",value="审批ID",example="1"),
        @ApiImplicitParam(required=false,name="isGoodsAll",value="物资是否完整",example="是"),
        @ApiImplicitParam(required=false,name="receiveImg",value="物资接收图片",example="1"),
        @ApiImplicitParam(required=false,name="remark",value="备注",example="1"),
    })
    @PostMapping({"/receviceGoods"})
    public ResponseEntity<BaseResponse<FyGoodsApply>> receviceGoods(@ApiIgnore FyGoodsApply fyGoodsApply) throws Exception
    { 
    	SecuryUserDetail user = UserContext.getUserSession();
    	 FyGoodsApply apply = applyService.selectFyGoodsApplyByApplyId(fyGoodsApply.getApplyId());
    	 if(apply==null) {
         	return BaseResponse.generateBadResponseEntity("记录无效", null);
         }
    	 
    	 if(!"3".equals(apply.getApplyStatus())) {
    		 return BaseResponse.generateBadResponseEntity("请先联系管理员受理!", null);
    	 }
    	 
    	 apply.setIsGoodsAll(fyGoodsApply.getIsGoodsAll());
    	 apply.setReceiveImg(fyGoodsApply.getReceiveImg());
    	 apply.setRemark(fyGoodsApply.getRemark());
    	 apply.setUpdateBy(user.getUserId()+"");
    	 apply.setUpdateTime(new Date());
    	 apply.setApplyStatus("4");
    	 apply.setApplyNode("已接收");
    	 
    	 applyService.updateFyGoodsApply(apply);
        
    	//插入审批记录
 		FyGoodsApplyHis fyGoodsApplyHis = new FyGoodsApplyHis();
 		fyGoodsApplyHis.setApplyId(apply.getApplyId());
 		fyGoodsApplyHis.setLiushuiNumber(apply.getLiushuiNumber());
 		fyGoodsApplyHis.setApplyNode(apply.getApplyNode());
 		fyGoodsApplyHis.setApplyStatus(apply.getApplyStatus());
 		fyGoodsApplyHis.setRemark(apply.getRemark());

		fyGoodsApplyHis.setCreateByName(user.getUserName());
		fyGoodsApplyHis.setCreateBy(user.getUserId()+"");
		fyGoodsApplyHis.setCreateTime(new Date());
		
		fyGoodsApplyHis.setUseTime(DateUtil.formatBetween(apply.getCreateTime(), apply.getUpdateTime(), Level.MINUTE));
 		
 		hisService.insertFyGoodsApplyHis(fyGoodsApplyHis);
        return BaseResponse.generateOKResponseEntity(apply);
    }
}
